package com.ruoyi.weixin.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.weixin.shop.domain.WxFloatButton;
import com.ruoyi.weixin.shop.domain.vo.WxFloatButtonAddBo;
import com.ruoyi.weixin.shop.domain.vo.WxFloatButtonEditBo;
import com.ruoyi.weixin.shop.domain.vo.WxFloatButtonQueryBo;
import com.ruoyi.weixin.shop.domain.vo.WxFloatButtonVo;
import com.ruoyi.weixin.shop.mapper.WxFloatButtonMapper;
import com.ruoyi.weixin.shop.service.IWxFloatButtonService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 悬浮按钮Service业务层处理
 *
 * @author ruoyi
 * @date 2021-07-20
 */
@Service
public class WxFloatButtonServiceImpl extends ServiceImpl<WxFloatButtonMapper, WxFloatButton> implements IWxFloatButtonService {

	@Override
	public WxFloatButtonVo queryById(Long id){
		return getVoById(id, WxFloatButtonVo.class);
	}

	@Override
	public TableDataInfo<WxFloatButtonVo> queryPageList(WxFloatButtonQueryBo bo) {
		PagePlus<WxFloatButton, WxFloatButtonVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), WxFloatButtonVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<WxFloatButtonVo> queryList(WxFloatButtonQueryBo bo) {
		return listVo(buildQueryWrapper(bo), WxFloatButtonVo.class);
	}

	private LambdaQueryWrapper<WxFloatButton> buildQueryWrapper(WxFloatButtonQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<WxFloatButton> lqw = Wrappers.lambdaQuery();
		lqw.like(StrUtil.isNotBlank(bo.getName()), WxFloatButton::getName, bo.getName());
		lqw.eq(StrUtil.isNotBlank(bo.getPath()), WxFloatButton::getPath, bo.getPath());
		lqw.like(StrUtil.isNotBlank(bo.getPathName()), WxFloatButton::getPathName, bo.getPathName());
		lqw.eq(bo.getPathType() != null, WxFloatButton::getPathType, bo.getPathType());
		lqw.eq(bo.getStyle() != null, WxFloatButton::getStyle, bo.getStyle());
		lqw.eq(StrUtil.isNotBlank(bo.getImage()), WxFloatButton::getImage, bo.getImage());
		lqw.eq(StrUtil.isNotBlank(bo.getBtnimage()), WxFloatButton::getBtnimage, bo.getBtnimage());
		lqw.eq(bo.getSort() != null, WxFloatButton::getSort, bo.getSort());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(WxFloatButtonAddBo bo) {
		WxFloatButton add = BeanUtil.toBean(bo, WxFloatButton.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(WxFloatButtonEditBo bo) {
		WxFloatButton update = BeanUtil.toBean(bo, WxFloatButton.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(WxFloatButton entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
